package com.wlanauth.web.action;

import java.math.BigDecimal;

import org.hibernate.criterion.Restrictions;

import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.domain.manage.Operator;
import com.wlanauth.domain.manage.UserHis;
import com.wlanauth.domain.radius.RadAcctHis;
import com.wlanauth.web.core.BaseActionSupport;
import com.wlanauth.web.core.JsonResult;
import com.wlanauth.web.util.RadiusUtils;
/**
 * 计费历史信息
 * @author zz
 * @date 2012-7-11
 * @email charles.zz.life@gmail.com
 */
public class RadAcctHisAction extends BaseActionSupport {
	
	private static final long serialVersionUID = 1L;
	private RadAcctHis entity;//实体类
	
	/**
	 * 用户流量列表
	 * @return
	 */
	
	public String listUserAcct(){
		//权限
		UserHis u = null;
		Operator oper = this.getCurrentOperator();
		if(oper == null){
			this.jsonResult = new JsonResult(this,"对不起，您无权查看该用户流量详情",false);
			return this.actionJsonResult();
		} 
		if(u == null){
			GeneralCriteria criteria = new GeneralCriteria(UserHis.class);
			criteria.add(Restrictions.eq("bindIP", entity.getUsername()));
			GeneralCriteriaResult<UserHis> result = criteria.executeQuery();
			if(result.getData()==null||result.getData().size()<1){
				this.jsonResult = new JsonResult(this,"用户不存在或已删除",false);
				return this.actionJsonResult();
			}
			u =  result.getData().get(0);
		}
		GeneralCriteria criteria = this.buildWebQueryCriteria(RadAcctHis.class);
		criteria.add(Restrictions.eq("username", u.getBindIP()));
		criteria.add(Restrictions.gt("acctStartTime", u.getActiveTime()));
		
		GeneralCriteriaResult<RadAcctHis> result = criteria.executeQuery();
		if(result.getData()!=null){
			for(RadAcctHis acct:result.getData()){
				Long inOct = acct.getAcctInputOctets()==null?0L:acct.getAcctInputOctets();
				Long outOct = acct.getAcctOutputOctets() == null?0L:acct.getAcctOutputOctets();
				acct.setAcctInputOctets(RadiusUtils.getViewOct(BigDecimal.valueOf(inOct)).longValue());
				acct.setAcctOutputOctets(RadiusUtils.getViewOct(BigDecimal.valueOf(outOct)).longValue());
				
			}
		}
		this.jsonResult = new JsonResult(this,result.getData(),result.getTotal());
		return this.actionJsonResult();
	}

	public RadAcctHis getEntity() {
		return entity;
	}

	public void setEntity(RadAcctHis entity) {
		this.entity = entity;
	}
	
}
